package edu.uit.se.laps.web.view;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.uit.se.laps.web.controller.ShoppingCart;
import edu.uit.se.laps.web.controller.TaiKhoanSession;
import edu.uit.se.laps.web.model.CTDonHang;
import edu.uit.se.laps.web.model.TaiKhoan;

public class GioHangAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2911290988625181035L;
	private HttpServletRequest request;
    private HttpServletResponse response;
    
    
	//private ShoppingCart cart;
	private List<CTDonHang> ctdhList;
	//private CTDonHangManager ctdhManager;
	private float sumMoney;
	private int idsp;
	
	/*
	 * Constructor
	 */
	public GioHangAction(){
		//ctdhManager = new CTDonHangManager();
	}
	
	
	/*
	 * Method
	 */
	public String execute(){
		HttpSession session = request.getSession();
		ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
		ctdhList = shopCart.getcTDHList();
		System.out.println("execute called!");
		
		return SUCCESS;
	}
	
	//check out gio hang
	public String checkOut(){
		//kiem tra user da dang nhap chua
		//neu chua thong bao loi, bat dang nhap
		HttpSession session = request.getSession();
		// get the taikhoan session
		TaiKhoanSession tkSession = (TaiKhoanSession) session
				.getAttribute("TaiKhoanSession");
		//tkSession.setEmail("");
		TaiKhoan tk = tkSession.getTk();
		if (tk != null) {
			ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
			//kiem tra user da dang nhap chua
			//neu chua thong bao loi, bat dang nhap
			
			//checkout
			sumMoney = shopCart.checkOut(tk.getEmail());
			ctdhList = shopCart.getcTDHList();
			System.out.println("checkout called!");
			
			return SUCCESS;
		}
		session.setAttribute("TaiKhoanSession", tkSession);
		return ERROR;
		
		
//		HttpSession session = request.getSession();
//		ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
//		
//		//checkout
//		sumMoney = shopCart.checkOut();
//		ctdhList = shopCart.getcTDHList();
//		System.out.println("checkout called!");
//		return SUCCESS;
		
	}
	
	//delete san pham tu gio hang
	public String deleteItemCart(){
		HttpSession session = request.getSession();
		ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
		//checkout
		shopCart.deleteItem(idsp);
		ctdhList = shopCart.getcTDHList();
		System.out.println("delete called!");
		
		return SUCCESS;
	}


	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}


	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}


	public List<CTDonHang> getCtdhList() {
		return ctdhList;
	}


	public void setCtdhList(List<CTDonHang> ctdhList) {
		this.ctdhList = ctdhList;
	}


	public float getSumMoney() {
		return sumMoney;
	}


	public void setSumMoney(float sumMoney) {
		this.sumMoney = sumMoney;
	}


	public int getIdsp() {
		return idsp;
	}


	public void setIdsp(int idsp) {
		this.idsp = idsp;
	}
}
